var myConfig = require('./config.js')
const envConfig = myConfig.getConfig();
const login = function (callBack) {
  wx.login({
    success: function (res) {     
      if (res.code) {
        wx.request({
          url: envConfig.api_host+'getOpenId',
          data: {
            code: res.code
          },
          isLoginApi:true,
          success: function (res) {
          
            var app = getApp();

            var cookie = res.header['Set-Cookie']          
            if (cookie != null) {
              app.globalData.app_cookie = cookie;
            }
            var rsdata = res.data;           
            if (rsdata.state) {
              var user = rsdata.user;
              var userInfo = rsdata.userInfo;
               var token = user.phone
               if (token != null && token!=""){
                wx.setStorageSync('token', token)
              }
              app.globalData.DHH_user = user;
              app.globalData.DHH_userInfo = userInfo;
              wx.setStorageSync('DHH_user', user);
              wx.setStorageSync('DHH_userInfo', userInfo);            
              typeof callBack == "function" && callBack({ user, userInfo })
            } else {
              wx.showToast({
                title: rsdata.msg,               
                duration: 2000
              })
            }
          }
        })
      } else {
        console.log('获取用户登录态失败！' + res.errMsg)
      }
    }
  });
}



module.exports = {
  login: login
}